package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.SevattalLeavesApplication;
import com.sevattal.service.leaves.entity.*;
import com.sevattal.service.leaves.entity.vo.*;
import com.sevattal.service.leaves.mapper.*;
import com.sevattal.service.leaves.service.InOutWarehouseOrderService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  出入库主表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class InOutWarehouseOrderServiceImpl  implements InOutWarehouseOrderService {

    @Autowired
    private InOutWarehouseMainOrderMapper mainOrderMapper;

    @Autowired
    private InOutWarehouseChildOrderMapper childOrderMapper;

    @Autowired
    private StockMasterMapper stockMasterMapper;

    @Autowired
    private ApplyForMainOrderMapper amOrderMapper;

    @Autowired
    private ReturnMainOrderMapper rmOrderMapper;

    @Override
    public boolean createInOutOrder(InOutWarehouseOrderVo inOutOrderVo) {
        JudgmentData(inOutOrderVo);
        QueryWrapper<InOutWarehouseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iomId",inOutOrderVo.getFIomid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.insert(getMainOrder(inOutOrderVo));
        List<InOutWarehouseChildOrder> childOrderList = getChildOrder(inOutOrderVo);
        for (int i=0;i<childOrderList.size();i++){
            childOrderMapper.insert(childOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean updateInOutOrder(InOutWarehouseOrderVo inOutOrderVo) {
        JudgmentData(inOutOrderVo);
        QueryWrapper<InOutWarehouseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iomId",inOutOrderVo.getFIomid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.update(getMainOrder(inOutOrderVo),mainOrderQueryWrapper);
        QueryWrapper<InOutWarehouseChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_iomId",inOutOrderVo.getFIomid());
        childOrderMapper.delete(childOrderQueryWrapper);
        List<InOutWarehouseChildOrder> childOrderList = getChildOrder(inOutOrderVo);
        for (int i=0;i<childOrderList.size();i++){
            childOrderMapper.insert(childOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean deleteInOutOrder(String id) {
        QueryWrapper<InOutWarehouseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iomId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        QueryWrapper<InOutWarehouseChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_iomId",id);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public List<InOutWarehouseOrderVo> getInOutOrderList(long page ,long limit, InOutWarehouseOrderVo inOutOrderVo) {
        QueryWrapper<InOutWarehouseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.orderByAsc("iom.f_iomId");
        mainOrderQueryWrapper.eq("iom.is_disabled",0);
        mainOrderQueryWrapper.eq("iom.is_deleted",0);
        if (inOutOrderVo.getFIomid() != null && !StringUtils.isEmpty(inOutOrderVo.getFIomid())){
            mainOrderQueryWrapper.like("iom.f_iomId",inOutOrderVo.getFIomid());
        }
        if (inOutOrderVo.getFWhid() != null && !StringUtils.isEmpty(inOutOrderVo.getFWhid())){
            mainOrderQueryWrapper.eq("iom.f_whId",inOutOrderVo.getFWhid());
        }
        if (inOutOrderVo.getFIsinout() != null && !StringUtils.isEmpty(inOutOrderVo.getFIsinout())){
            mainOrderQueryWrapper.eq("iom.f_isInOut",inOutOrderVo.getFIsinout());
        }
        if (inOutOrderVo.getSearchStartDate() != null){
            mainOrderQueryWrapper.ge("iom.f_iomDate",inOutOrderVo.getSearchStartDate());
        }
        if (inOutOrderVo.getSearchEndDate() != null){
            mainOrderQueryWrapper.le("iom.f_iomDate",inOutOrderVo.getSearchEndDate());
        }
        List<InOutWarehouseMainOrderVo> mainOrderVos = mainOrderMapper.getInOutMainOrderList((page-1)*limit, limit,mainOrderQueryWrapper);
        QueryWrapper<InOutWarehouseChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        List<InOutWarehouseOrderVo> orderVoList = new ArrayList<>();
        for (int i=0;i<mainOrderVos.size();i++){
            childOrderQueryWrapper.eq("ioc.f_iomId",mainOrderVos.get(i).getFIomid());
            childOrderQueryWrapper.eq("ioc.is_deleted",0);
            childOrderQueryWrapper.eq("ioc.is_disabled",0);
            List<InOutWarehouseChildOrderVo> childOrderVos = childOrderMapper.getInOutChildOrderList(childOrderQueryWrapper);
            orderVoList.add(getVo(mainOrderVos.get(i),childOrderVos));
            childOrderQueryWrapper.clear();
        }
        return orderVoList;
    }

    @Override
    public boolean confirmInOutOrder(String id) {
        QueryWrapper<InOutWarehouseMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iomId",id);
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        InOutWarehouseMainOrder mainOrder = mainOrderMapper.selectOne(mainOrderQueryWrapper);
        if (StringUtils.equals("1",mainOrder.getFIomconfirm())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }else {
            mainOrder.setFIomconfirm("1");
            mainOrderMapper.update(mainOrder,mainOrderQueryWrapper);
        }
        QueryWrapper<InOutWarehouseChildOrder> childOrderQueryWrapper= new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_iomId",id);
        List<InOutWarehouseChildOrder> childOrders = childOrderMapper.selectList(childOrderQueryWrapper);
        if (childOrders.size() == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<StockMaster> stockMasterQueryWrapper = new QueryWrapper<>();
        for (int i=0;i<childOrders.size();i++){
            stockMasterQueryWrapper.eq("f_whId",mainOrder.getFWhid());
            stockMasterQueryWrapper.eq("f_mId",childOrders.get(i).getFMid());
            int sm_count = stockMasterMapper.selectCount(stockMasterQueryWrapper);
            if (sm_count > 1){
                throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
            }
            StockMaster stockMaster = stockMasterMapper.selectOne(stockMasterQueryWrapper);
            if ( sm_count == 0){
                stockMasterMapper.insert(getSockMaster(stockMaster,mainOrder,childOrders.get(i)));
            }else if (sm_count == 1){
                stockMasterMapper.update(getSockMaster(stockMaster,mainOrder,childOrders.get(i)),stockMasterQueryWrapper);
            }
        }
        // 更新申领单表
        ApplyForMainOrder applyForMainOrder = new ApplyForMainOrder();
        if (mainOrder.getFAmid() != null && !StringUtils.isEmpty(mainOrder.getFAmid())) {
            QueryWrapper<ApplyForMainOrder> applyForMainOrderQueryWrapper = new QueryWrapper<>();
            applyForMainOrderQueryWrapper.eq("f_amId",mainOrder.getFAmid());
            applyForMainOrder.setFAmstatus("2");
            amOrderMapper.update(applyForMainOrder,applyForMainOrderQueryWrapper);
        }
        // 更新归还表
        ReturnMainOrder returnMainOrder = new ReturnMainOrder();
        if (mainOrder.getFRmid() != null && !StringUtils.isEmpty(mainOrder.getFRmid())){
            QueryWrapper<ReturnMainOrder> returnMainOrderQueryWrapper = new QueryWrapper<>();
            returnMainOrderQueryWrapper.eq("f_rmId",mainOrder.getFRmid());
            returnMainOrder.setFRmstatus("1");
            rmOrderMapper.update(returnMainOrder,returnMainOrderQueryWrapper);
        }
        return true;
    }
    // 获得出入库对象
    private StockMaster getSockMaster(StockMaster stockMaster,InOutWarehouseMainOrder mainOrder, InOutWarehouseChildOrder childOrder) {
        StockMaster result = new StockMaster();
        result.setFWhid(mainOrder.getFWhid());
        result.setFMid(childOrder.getFMid());
        if (stockMaster == null){
            stockMaster = new StockMaster();
            stockMaster.setFSmqty(0);
        }
        if (StringUtils.equals("0",mainOrder.getFIsinout()) || StringUtils.isEmpty(mainOrder.getFIsinout()) || mainOrder.getFIsinout() == null){
            result.setFSmqty(childOrder.getFIocquantity() + stockMaster.getFSmqty());
        }else {
            result.setFSmqty(childOrder.getFIocquantity() - stockMaster.getFSmqty());
        }

        return  result;
    }

    // 判断数据是否正确
    public void JudgmentData(InOutWarehouseOrderVo orderVo){
        if (orderVo.getInOutWarehouseChildOrderVoList().size() == 0 || orderVo.getFIomid() == null || StringUtils.isEmpty(orderVo.getFIomid())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    // 获取主表数据
    public InOutWarehouseMainOrder getMainOrder(InOutWarehouseOrderVo orderVo){
        InOutWarehouseMainOrder mainOrder = new InOutWarehouseMainOrder();
        mainOrder.setFIomid(orderVo.getFIomid());
        mainOrder.setFWhid(orderVo.getFWhid());
        mainOrder.setFEid(orderVo.getFEid());
        mainOrder.setFIsinout(orderVo.getFIsinout());
        mainOrder.setFPmid(orderVo.getFPmid());
        mainOrder.setFPamid(orderVo.getFPamid());
        mainOrder.setFIamid(orderVo.getFIamid());
        mainOrder.setFAmid(orderVo.getFAmid());
        mainOrder.setFRmid(orderVo.getFRmid());
        mainOrder.setFIomdate(orderVo.getFIomdate());
        mainOrder.setFIomstatus(orderVo.getFIomstatus());
        mainOrder.setFIomconfirm(orderVo.getFIomconfirm());
        mainOrder.setFIommemo(orderVo.getFIommemo());
        return mainOrder;
    }

    // 获取子表数据
    public List<InOutWarehouseChildOrder> getChildOrder(InOutWarehouseOrderVo orderVo){
        List<InOutWarehouseChildOrder> childOrderList = new ArrayList<>();
        InOutWarehouseChildOrder childOrder_temp = new InOutWarehouseChildOrder();
        for (int i=0;i<orderVo.getInOutWarehouseChildOrderVoList().size();i++){
            childOrder_temp.setFIomid(orderVo.getFIomid());
            childOrder_temp.setFMid(orderVo.getInOutWarehouseChildOrderVoList().get(i).getFMid());
            childOrder_temp.setFIocquantity(orderVo.getInOutWarehouseChildOrderVoList().get(i).getFIocquantity());
            childOrder_temp.setFIocdate(orderVo.getInOutWarehouseChildOrderVoList().get(i).getFIocdate());
            childOrder_temp.setFIocstatus(orderVo.getInOutWarehouseChildOrderVoList().get(i).getFIocstatus());
            childOrder_temp.setFIocmemo(orderVo.getInOutWarehouseChildOrderVoList().get(i).getFIocmemo());
            childOrderList.add(childOrder_temp);
        }
        return  childOrderList;
    }

    // 获取Vo
    public InOutWarehouseOrderVo getVo(InOutWarehouseMainOrderVo mainOrderVo, List<InOutWarehouseChildOrderVo> childOrderVos){
        InOutWarehouseOrderVo orderVo = new InOutWarehouseOrderVo();
        orderVo.setFIomid(mainOrderVo.getFIomid());
        orderVo.setFWhid(mainOrderVo.getFWhid());
        orderVo.setFWhname(mainOrderVo.getFWhname());
        orderVo.setFEid(mainOrderVo.getFEid());
        orderVo.setFEname(mainOrderVo.getFEname());
        orderVo.setFPmid(mainOrderVo.getFPmid());
        orderVo.setFPamid(mainOrderVo.getFPamid());
        orderVo.setFIsinout(mainOrderVo.getFIsinout());
        orderVo.setFIamid(mainOrderVo.getFIamid());
        orderVo.setFAmid(mainOrderVo.getFAmid());
        orderVo.setFRmid(mainOrderVo.getFRmid());
        orderVo.setFIomdate(mainOrderVo.getFIomdate());
        orderVo.setFIomconfirm(mainOrderVo.getFIomconfirm());
        orderVo.setFIommemo(mainOrderVo.getFIommemo());
        orderVo.setInOutWarehouseChildOrderVoList(childOrderVos);
        return orderVo;
    }
}
